﻿using System;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;
using System.Collections.Generic;
using System.Linq;

namespace HealthCommunity.Controls
{
	public partial class UCMemberList : System.Web.UI.UserControl
	{
		private IUserProvider _UserProvider;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindUser();
			}
		}

		protected void gvMember_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.DataItem != null)
			{
				var dataItem = e.Row.DataItem as User;

				var ltrSex = e.Row.FindControl("ltrSex") as Literal;
				var ltrUserState = e.Row.FindControl("ltrUserState") as Literal;
				var lbtnDelete = e.Row.FindControl("lbtnDelete") as LinkButton;
				var hlUserDetail = e.Row.FindControl("hlUserDetail") as HyperLink;

				if (dataItem.Sex.Equals(1))
				{
					ltrSex.Text = "男";
				}
				else if (dataItem.Sex.Equals(2))
				{
					ltrSex.Text = "女";
				}
				else if (dataItem.Sex.Equals(0))
				{
					ltrSex.Text = "保密";
				}

				if (dataItem.UserState.Equals((int)UserState.Active))
				{
					ltrUserState.Text = "活动";
				}
				else if (dataItem.UserState.Equals((int)UserState.Deleted))
				{
					ltrUserState.Text = "已删除";
				}

				if(dataItem.UserState.Equals((int)UserState.Deleted))
				{
					lbtnDelete.Text = "恢复";
					lbtnDelete.CommandName = "recover";
				}
				else
				{
					lbtnDelete.Text = "删除";
					lbtnDelete.CommandName = "remove";
				}

				hlUserDetail.NavigateUrl = "~/MemberDetail.aspx?id=" + dataItem.UserID.ToString();
			}
		}

		protected void gvMember_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName.Equals("remove"))
			{
				var userID = int.Parse(e.CommandArgument.ToString());

				if (_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				var theUser = _UserProvider.GetUser(userID, true);

				if (theUser != null)
				{
					theUser.UserState = (int)UserState.Deleted;

					if (_UserProvider.UpdateUser(theUser, false))
					{
						BindUser();
					}
					else
					{
						ltrMsg.Text = "操作失败了";
					}
				}
			}

			if(e.CommandName.Equals("recover"))
			{
				var userID = int.Parse(e.CommandArgument.ToString());

				if(_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				var theUser = _UserProvider.GetUser(userID, true);

				if(theUser != null)
				{
					theUser.UserState = (int)UserState.Active;

					if(_UserProvider.UpdateUser(theUser, false))
					{
						BindUser();
					}
					else
					{
						ltrMsg.Text = "操作失败了";
					}
				}
			}

			if (e.CommandName.Equals("deletecomplete"))
			{
				var userID = int.Parse(e.CommandArgument.ToString());

				if (_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				if (_UserProvider.DeleteUser(userID))
				{
					BindUser();
				}
				else
				{
					ltrMsg.Text = "操作失败了";
				}
			}
		}

		private void BindUser()
		{
			var user = SiteContext.Current.CurrentUser;

			if (user != null)
			{
				if (_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				if (user.DrugStoreID.GetValueOrDefault(0) > 0)
				{
					var storemember = _UserProvider.GetUserByRole("storemember", user.DrugStoreID.Value);
					var commonuser = _UserProvider.GetUserByRole("commonuser", user.DrugStoreID.Value);

					List<User> data = new List<User>();

					if(storemember != null && storemember.Count > 0)
					{
						data.AddRange(storemember);
					}

					if(commonuser != null && commonuser.Count > 0)
					{
						data.AddRange(commonuser);
					}

					gvMember.DataSource = data;
					gvMember.DataBind();
				}
			}
		}
	}
}